Page({
  data: {
    id: undefined,
    houseInfo: '',
    url: '',
    validTime: '',
    encryptedData: ''
  },
  onLoad(option) {
    let { id, encryptedData } = option

    this.setData({ id, encryptedData })
    if (!!id) {
      this.getDesc(id)
    }
    if (!!encryptedData) {
      this.getShareDesc(encryptedData)
    }

  },
  async getDesc(id) {
    let { houseInfo, url, validTime, encryptedData } = await wx.http.get('/visitor/' + id)
    this.setData({
      houseInfo,
      url,
      validTime,
      encryptedData
    })

  },
  async getShareDesc(encryptedData) {
    let { houseInfo, url, validTime } = await wx.http.get('/visitor/share/' + encryptedData)
    this.setData({
      houseInfo,
      url,
      validTime,
      encryptedData
    })
  },
  async save() {
    let res = await wx.getImageInfo({
      src: this.data.url
    })
    wx.saveImageToPhotosAlbum({
      filePath: res.path,
      complete: data => {
        if (data.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          wx.showModal({
            title: '提示',
            content: '请允许授权此功能',
            async success(res) {
              if (res.confirm) {
                await wx.openSetting()
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.encryptedData,
      imageUrl: this.data.url,
    }
  },
})
